package com.joysuch.wwyt.attendant.job;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.attendant.bean.AttendantStayTimeBean;
import com.joysuch.wwyt.attendant.bean.vo.AttendantTimeVo;
import com.joysuch.wwyt.attendant.bean.vo.PersonStayTimeReq;
import com.joysuch.wwyt.attendant.bean.vo.PersonStayTimeResp;
import com.joysuch.wwyt.attendant.entity.AttendantRecord;
import com.joysuch.wwyt.attendant.service.AttendantRecordService;
import com.joysuch.wwyt.attendant.service.OpenPlatformService;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.common.util.StringUtil;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Description //TODO
 * @Date 2023/2/21 18:09
 * @Author Leong
 **/
@Slf4j
@AllArgsConstructor
@Component
public class PlanStayTimeJob {

    @Resource
    private AttendantRecordService attendantRecordService;

    @Resource
    private OpenPlatformService openPlatformService;

    /**
     * 围栏停留时间接口限制五分钟后请求
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void planStateSyncJob() {
        //查询停留时间为空的计划
        List<AttendantStayTimeBean> list = attendantRecordService.findNoDuration();
        List<AttendantRecord> records = Lists.newArrayList();
        for (AttendantStayTimeBean bean : list){
            if (StringUtils.isBlank(bean.getSn())) {
                continue;
            }
            AttendantRecord record = new AttendantRecord();
            record.setId(bean.getId());
            record.setTenentId(bean.getTenentId());
            record.setOrgCode(bean.getOrgCode());
            //获取停留时常
            PersonStayTimeReq req = new PersonStayTimeReq();
            req.setMacs(Lists.newArrayList(bean.getSn()));
            req.setRailId(bean.getPlatformAreaId());
            req.setStartTime(bean.getSignInTime().getTime());
            req.setEndTime(bean.getFinishTime().getTime());
            try {
                List<PersonStayTimeResp> stayTimeResps = openPlatformService.railStayTime(req,bean.getTenentId(),bean.getOrgCode());
                if (CollectionUtils.isNotEmpty(stayTimeResps)) {
                    List<AttendantTimeVo> vos=new ArrayList<>();
                    long duration=0;
                    for (PersonStayTimeResp stayTimeResp : stayTimeResps) {
                        if (stayTimeResp != null&&stayTimeResp.getDuration()>0){
                            if (stayTimeResp.getDuration()>0) {
                                duration=  duration+stayTimeResp.getDuration();
                            }
                            AttendantTimeVo attendantTimeVo = new AttendantTimeVo();
                            if (stayTimeResp.getDurationEndTime()>0) {
                                attendantTimeVo.setOutTime(new Date(stayTimeResp.getDurationEndTime()));
                            }
                            if (stayTimeResp.getDurationStartTime()>0) {
                                attendantTimeVo.setInTime(new Date(stayTimeResp.getDurationStartTime()));
                                vos.add(attendantTimeVo);
                            }
                        }
                    }
                    SimpleDateFormat dateFormat = SimpleDateFormatCache.getHms();
                    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
                    if (CollectionUtils.isNotEmpty(vos)) {
                        record.setAccessTime(JSON.toJSONString(vos));
                    }
                    if (duration>0){
                        record.setDuration(dateFormat.format(duration));
                    }
                    records.add(record);
                }

            } catch (ApiCoreException e) {
                throw new RuntimeException(e);
            }
        }
        if (CollectionUtils.isNotEmpty(records)){
            for (AttendantRecord record : records) {
                attendantRecordService.updateById(record);
            }
        }
    }
}
